package mest.controller.LGF.PlanPurchaseMoney;

import mest.VO.LGF.PlanPurchaseMoney.plan_purchasemoneyrecord;
import mest.entity.User;
import mest.service.LGF.PlanPurchaseMoney.IPlanPurchaseMoneyService;
import mest.util.ResultObj;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-12-18 15:15
 */
//Model是属于请求域
    //           @RequestMapping(value="/one",params = "method=a")
   //           @RequestMapping(value="/two1",produces = {"application/json;charset=utf-8","text/plain;charset=utf-8"})
    //参数:@RequestParam(value = "b",required = false,defaultValue = "握草尼玛")
    //         @RequestBody String(str) 输出:userCode=admin&userPassword=1234567
    //          @SessionAttributes({"a","b","c"}) 自动把该controller下所有返回页面的属性的作用域改成session
    //          @ModelAttribute标注一个方法时，指定每次跳入某个路径时该方法先执行
@CrossOrigin
@RestController
@RequestMapping(value="/plan_purchaseMoney")
public class PlanPurchaseMoneyController {

    @Resource
    private IPlanPurchaseMoneyService ipms;

    @RequestMapping(value="/baseSelectNofinish", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public Map baseSelectNofinish(int page,int limit){
        Map map = ipms.baseSelectNofinish(page, limit);

        return map;
    }


    @RequestMapping(value="/baseSelectFinish", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public Map baseSelectFinish(int page,int limit){
        Map map = ipms.baseSelectFinish(page, limit);
        return map;
    }

    //路径 /planMoneyFlow/alert/payMoney.html

    @PostMapping(value="/addRecord", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String addRecord(plan_purchasemoneyrecord ppmr, HttpServletRequest request){

        User user = (User) request.getSession().getAttribute("user");
        ppmr.setEfId(user.getUserId());

        System.out.println(ppmr);
        int i = ipms.addMoneyRecord(ppmr);

        if(i == -2){
          return   "服务器异常";
        }else if(i == -1){
            return "插入失败";
        }else if(i>0){
            return "插入成功";
        }

        return "未知错误，请联系管理员";
    }
    @RequestMapping(value="/pastRecord", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public Map pastRecord(int ppmId,int page,int limit){
        Map map = ipms.selectPastRecord(ppmId,page,limit);
        return map;
    }


    @RequestMapping(value="/finish", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String finish(int ppmId,int status){

        int i = ipms.updateByPPMId(ppmId, status);
        if(i == -2){
            return   "服务器异常";
        }else if(i == -1){
            return "更新失败";
        }else if(i>0){
            return "更新成功";
        }
        return "未知错误，请联系管理员";
    }


    @RequestMapping(value="/nofinish", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String nofinish(int ppmId,int status){

        int i = ipms.updateByPPMId(ppmId, status);
        if(i == -2){
            return   "服务器异常";
        }else if(i == -1){
            return "更新失败";
        }else if(i>0){
            return "更新成功";
        }
        return "未知错误，请联系管理员";
    }

    //根据poId生成计划采购付款单
    @RequestMapping(value="/insertPlanPurchaseMoneyOrdrByPOID", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public ResultObj insertPlanPurchaseMoneyOrdrByPOID( int poId){

        try {
            int i = ipms.insertPlanPurchaseMoneyOrdrByPOID(poId );
            if(i == -2){
                return   ResultObj.createErrorMsg(ResultObj.ERROR2);
            }else if(i == -1){
                return   ResultObj.createErrorMsg(ResultObj.ERROR);
            }else if (i>0){
                return   ResultObj.createSuccessMsg(ResultObj.UPDATE_SUCCESS);
            }else{
                return   ResultObj.createErrorMsg(ResultObj.ERROR2);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return   ResultObj.createErrorMsg(ResultObj.ERROR2);
        }

    }
}
